package com.yuju.controller.login;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.login.enums.RoleMenuReturnCodeEnum;
import com.yuju.login.model.RoleMenuModel;
import com.yuju.login.service.IRoleMenuService;
import com.yuju.util.Utils;
import com.yuju.vo.login.RoleMenuVO;

/**
 * 
 * 类名：RoleMenuController  <br />
 *
 * 功能：
 *
 * @author : zhousc <br />
 * @Date : 2017年12月4日 上午11:02:27  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("roleMenu")
public class RoleMenuController extends BaseController<RoleMenuVO, RoleMenuModel> {

	@Autowired
	private IRoleMenuService roleMenuService;
	
	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午11:02:55 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj) {
		RoleMenuVO  roleMenuVO = obj.toJavaObject(RoleMenuVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roleMenuVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = roleMenuService.pageList(convert(roleMenuVO), roleMenuVO.getPage(), roleMenuVO.getPageSize());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoleMenuReturnCodeEnum.ROLEMENU_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoleMenuReturnCodeEnum.ROLEMENU_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午11:02:58 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj) {
		RoleMenuVO  roleMenuVO = obj.toJavaObject(RoleMenuVO.class);
		Result result = new Result();
		int rows = roleMenuService.save(convert(roleMenuVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoleMenuReturnCodeEnum.ROLEMENU_SAVE_SUCCESS.getCode());
			result.setReturnMsg(RoleMenuReturnCodeEnum.ROLEMENU_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoleMenuReturnCodeEnum.ROLEMENU_SAVE_FAILURE.getCode());
			result.setReturnMsg(RoleMenuReturnCodeEnum.ROLEMENU_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午11:03:01 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj) {
		RoleMenuVO  roleMenuVO = obj.toJavaObject(RoleMenuVO.class);
		Result result = new Result();
		int rows = roleMenuService.updateSelective(convert(roleMenuVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoleMenuReturnCodeEnum.ROLEMENU_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(RoleMenuReturnCodeEnum.ROLEMENU_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoleMenuReturnCodeEnum.ROLEMENU_UPDATE_FAILURE.getCode());
			result.setReturnMsg(RoleMenuReturnCodeEnum.ROLEMENU_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午11:03:05 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj) {
		RoleMenuVO  roleMenuVO = obj.toJavaObject(RoleMenuVO.class);
		Result result = new Result();
		int rows = roleMenuService.del(roleMenuVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoleMenuReturnCodeEnum.ROLEMENU_DEL_SUCCESS.getCode());
			result.setReturnMsg(RoleMenuReturnCodeEnum.ROLEMENU_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoleMenuReturnCodeEnum.ROLEMENU_DEL_FAILURE.getCode());
			result.setReturnMsg(RoleMenuReturnCodeEnum.ROLEMENU_DEL_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午11:03:08 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj) {
		RoleMenuVO  roleMenuVO = obj.toJavaObject(RoleMenuVO.class);
		Result result = new Result();
		RoleMenuModel RoleMenuModel = roleMenuService.findById(roleMenuVO.getId());
		if (Utils.isNotNull(RoleMenuModel)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(RoleMenuModel);
			result.setReturnCode(RoleMenuReturnCodeEnum.ROLEMENU_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoleMenuReturnCodeEnum.ROLEMENU_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
}
